package com.newcapec.modules.pass.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newcapec.common.exception.RRException;
import com.newcapec.common.qrcode.QRCodeUtil;
import com.newcapec.common.utils.ConfigConstant;
import com.newcapec.common.utils.PageUtils;
import com.newcapec.common.utils.Query;
import com.newcapec.modules.health.entity.CustomerEntity;
import com.newcapec.modules.health.service.CustomerService;
import com.newcapec.modules.oss.cloud.OSSFactory;
import com.newcapec.modules.pass.dao.GateInfoDao;
import com.newcapec.modules.pass.entity.GateInfoEntity;
import com.newcapec.modules.pass.entity.VenueInfoEntity;
import com.newcapec.modules.pass.service.GateInfoService;
import com.newcapec.modules.pass.service.VenueInfoService;
import com.newcapec.modules.sys.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("gateInfoService")
public class GateInfoServiceImpl extends ServiceImpl<GateInfoDao, GateInfoEntity> implements GateInfoService {

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private VenueInfoService venueInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GateInfoEntity> page = this.baseMapper.selectPageVo(
                new Query<GateInfoEntity>().getPage(params, "create_time", false), params
        );
        String ossDomain = OSSFactory.getOssDomain();
        page.getRecords().stream().forEach(item -> item.setQrCode(ossDomain + item.getQrCode()));

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(GateInfoEntity gateInfo) {
        this.save(gateInfo);
        //生成二维码
        String url = getQrString(gateInfo);
        gateInfo.setQrCode(url);
        boolean result = this.updateById(gateInfo);
        List<Long> listIds = gateInfo.getListIds();
        if(listIds!=null && !listIds.isEmpty()){
            this.baseMapper.deleteWhiteList(gateInfo.getId());
            this.baseMapper.insertWhiteList(gateInfo);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(GateInfoEntity gateInfo) {
        //生成二维码
        String url = getQrString(gateInfo);
        gateInfo.setQrCode(url);
        boolean result = this.updateById(gateInfo);
        List<Long> listIds = gateInfo.getListIds();
        if(listIds!=null){
            this.baseMapper.deleteWhiteList(gateInfo.getId());
            if(!listIds.isEmpty()){
                this.baseMapper.insertWhiteList(gateInfo);
            }
        }
        return result;
    }

    @Override
    public GateInfoEntity get(Long id) {
        GateInfoEntity gateInfo = this.getById(id);
        String ossDomain = OSSFactory.getOssDomain();
        gateInfo.setQrCode(ossDomain + gateInfo.getQrCode());
        VenueInfoEntity venueInfo = venueInfoService.getById(gateInfo.getVenueId());
        gateInfo.setVenueName(venueInfo.getVenueName());
        List<Long> listIds = this.baseMapper.selectWhiteList(id);
        gateInfo.setListIds(listIds);
        return gateInfo;
    }

    private String getQrString(GateInfoEntity gateInfo){
        CustomerEntity customer = customerService.get(gateInfo.getCustomerId());
        // 嵌入二维码的图片路径
        String logo = customer.getLogo();
        try {
            // 存放在二维码中的内容
            String text = sysConfigService.getValue(ConfigConstant.QRCODE_URL) +
                    "?gateId=" + gateInfo.getId() +
                    "&type=" + gateInfo.getType() +
                    "&source=" + (StringUtils.isNotBlank(gateInfo.getMachinesType()) ? gateInfo.getMachinesType() : "f961") +
                    "&mId=" + (StringUtils.isNotBlank(gateInfo.getMachinesId()) ? gateInfo.getMachinesId() : "0");
            //生成二维码
            return QRCodeUtil.encodeAndUpload(text, logo, true);
        } catch (Exception e) {
            throw new RRException("生成二维码有误", e);
        }
    }
}